package org.dfzt.common.system.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.dfzt.common.constant.CommonConstant;
import org.dfzt.common.system.service.IUploadService;
import org.dfzt.common.util.MinioUtil;
import org.dfzt.common.util.oss.OssBootUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author furao
 * @desc
 * @date 2020/5/26
 * @package org.dfzt.common.system.service.impl
 */
@Service
public class IUploadServiceImpl implements IUploadService {

    private static final Logger log = LoggerFactory.getLogger(IUploadServiceImpl.class);

    @Override
    public String upload(MultipartFile file, String bizPath, String uploadType) {
        String url = "";
        if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){

            try {
                //校验文件大小
                boolean check = this.fileCheck(file,file.getOriginalFilename());
                if(check){
                    url = MinioUtil.upload(file,bizPath);
                    if(StringUtils.isNotBlank(url)){
                        URL newurl = new URL(url);
                        String[] names = newurl.getFile().split("/");
                        //获取文件外链，默认有效时间1800s
                        url = MinioUtil.getObjectURL(names[2]+"/"+names[3],CommonConstant.UPLOAD_EFFECTIVE_SECONDS);
                    }
                }else{
                    return "文件大小超过限制！";
                }
            } catch (MalformedURLException e) {
                log.info("url获取失败");
                e.printStackTrace();
            }

        }else{
            url = OssBootUtil.upload(file,bizPath);
        }
        return url;
    }

    @Override
    public void removeFile(String fileName, String bizPath){
        MinioUtil.removeObject(bizPath+"/"+fileName);
    }

    @Override
    public String getObjectURL(String fileName, String bizPath, Integer expires) {
        String url = MinioUtil.getObjectURL(bizPath+"/"+fileName,expires);
        return url;
    }

    @Override
    public InputStream getMinioFile(String fileName, String bizPath) {
        InputStream file = MinioUtil.getMinioFile(bizPath+"/"+fileName);
        return file;
    }

    /**判断是否是图片*/
    private boolean isImage(String fileName) {
        //设置允许上传文件类型
        String suffixList = "jpg,gif,png,ico,bmp,jpeg";
        // 获取文件后缀
        String suffix = fileName.substring(fileName.lastIndexOf(".")
                + 1, fileName.length());
        return suffixList.contains(suffix.trim().toLowerCase());
    }

    /**验证文件大小 */
    private boolean fileCheck(MultipartFile upfile,String fileName){
        boolean check = false;
        long size = upfile.getSize();
        if(isImage(fileName)){
            if(size > MinioUtil.getImgSize()){
                log.info("上传对图片大于：{}M限制",(MinioUtil.getImgSize() / 1024 / 1024) );
            }else{
                check = true;
            }
        }else{
            if(size > MinioUtil.getFileSize()){
                log.info("上传对文件大于：{}M限制",(MinioUtil.getFileSize() / 1024 / 1024));
            }else{
                check = true;
            }
        }
        return check;
    }
}
